package com.llyb.tingshu.search.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class ThreadPoolExecutorConfig {

    @Bean
    public ThreadPoolExecutor threadPoolExecutor() {
        int coreNum = Runtime.getRuntime().availableProcessors();
        //线程池创建,核心线程同时创建
        //threadPoolExecutor.prestartCoreThread();
//        threadPoolExecutor.prestartAllCoreThreads();
        return new ThreadPoolExecutor(
                coreNum*2,
                coreNum*2,
                3,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(100),
                Executors.defaultThreadFactory(),
                (runnable,executor)->{
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        log.warn("线程池被拒绝了");
                    }
                    executor.submit(runnable);
                }
        );
    }
}
